import { event } from "vue-gtag";

export function useLoadAdxReward(id, config = { emptyCallback, playCallback, endCallback, closeCallback }) {
    const ADX_REWARD_ID = id
    return {
        init: () => {
            try {
                googletag = window.googletag || { cmd: [] };
            } catch (error) {
                console.error(error)
            }
            const rewardedSlot = googletag.defineOutOfPageSlot(
                ADX_REWARD_ID,
                googletag.enums.OutOfPageFormat.REWARDED)
            googletag.cmd.push(() => {

                const slotRenderEnded = (e) => {
                    if (e.slot === rewardedSlot && e.isEmpty != false) {
                        config.emptyCallback && config.emptyCallback(e)
                    }
                    googletag.pubads().removeEventListener('slotRenderEnded', slotRenderEnded);
                }

                const rewardedSlotReady = (e) => {
                    config.playCallback && config.playCallback(e)
                    e.makeRewardedVisible()
                    console.log('ad ready')
                    googletag.pubads().removeEventListener('rewardedSlotReady', rewardedSlotReady);
                }

                const rewardedSlotGranted = (e) => {
                    config.endCallback && config.endCallback(e)
                    console.log('ad reward grant')
                    googletag.pubads().removeEventListener('rewardedSlotGranted', rewardedSlotGranted);
                }

                const rewardedSlotClosed = (e) => {
                    config.closeCallback && config.closeCallback(e)
                    googletag.destroySlots([rewardedSlot]);
                    console.log('ad close')
                    googletag.pubads().removeEventListener('rewardedSlotClosed', rewardedSlotClosed);
                }

                if (rewardedSlot) {
                    rewardedSlot.addService(googletag.pubads());
                    // 当激励广告请求渲染结束时会触发此事件
                    googletag.pubads().addEventListener('slotRenderEnded', slotRenderEnded);
                    // 当激励广告准备好展示时会触发此事件
                    googletag.pubads().addEventListener('rewardedSlotReady', rewardedSlotReady);
                    // 当观看激励广告获得奖励时会触发此事件
                    googletag.pubads().addEventListener('rewardedSlotGranted', rewardedSlotGranted);
                    // 用户关闭激励广告位时会触发此事件
                    googletag.pubads().addEventListener('rewardedSlotClosed', rewardedSlotClosed);
                    googletag.enableServices();
                    googletag.display(rewardedSlot);
                }
            });
        },
        destroy: () => {
            googletag = window.googletag || { cmd: [] };
            const rewardedSlot = googletag.defineOutOfPageSlot(
                ADX_REWARD_ID,
                googletag.enums.OutOfPageFormat.REWARDED)
            googletag.destroySlots([rewardedSlot]);
            console.log('ad destroy')
        }
    }
}

export function usePageSlotAd(id) {
    window.googletag = window.googletag || { cmd: [] };
    googletag.cmd.push(function () {
        var slot = googletag.defineOutOfPageSlot(id, googletag.enums.OutOfPageFormat.INTERSTITIAL);
        if (slot) slot.addService(googletag.pubads());
        googletag.enableServices();
        googletag.display(slot);
    })
}